package com.quanshu.shop.order.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.order.adapter.OrderListPagerAdapeter;
import com.quanshu.shop.order.entity.OrderlistRlEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderListActivity extends BaseActivity {

    @BindView(R.id.order_title)
    BaseTitleBar orderTitle;
    @BindView(R.id.rl_v)
    RecyclerView rlV;
    private List<Fragment> fragmentList;
    private List<OrderlistRlEntity> orderlistRlEntityList;
    private OrderListPagerAdapeter orderListPagerAdapeter;
    private int state = 0;
    private HashMap<Integer, Fragment> sortItemFragmentHashMap;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        ImmersionBar.with(this).titleBar(orderTitle).statusBarDarkFont(true).statusBarColor("#ffffff").init();
        orderTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("我的订单").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        if (getIntent().getExtras()!=null){
            state=getIntent().getExtras().getInt("ORDER_FLAG");
        }
        fragmentManager = getSupportFragmentManager();
        sortItemFragmentHashMap = new HashMap<>();
        sortItemFragmentHashMap.put(0, OrderList1Fragment.newInstance());
        sortItemFragmentHashMap.put(1, OrderList2Fragment.newInstance());
        sortItemFragmentHashMap.put(2, OrderList3Fragment.newInstance());
        sortItemFragmentHashMap.put(3, OrderList4Fragment.newInstance());
        sortItemFragmentHashMap.put(4, OrderList5Fragment.newInstance());
        fragmentManager.beginTransaction()
                .add(R.id.fl1, sortItemFragmentHashMap.get(0), String.valueOf(0))
                .add(R.id.fl1, sortItemFragmentHashMap.get(1), String.valueOf(1))
                .add(R.id.fl1, sortItemFragmentHashMap.get(2), String.valueOf(2))
                .add(R.id.fl1, sortItemFragmentHashMap.get(3), String.valueOf(3))
                .add(R.id.fl1, sortItemFragmentHashMap.get(4), String.valueOf(4))
                .hide(sortItemFragmentHashMap.get(0))
                .hide(sortItemFragmentHashMap.get(1))
                .hide(sortItemFragmentHashMap.get(2))
                .hide(sortItemFragmentHashMap.get(3))
                .hide(sortItemFragmentHashMap.get(4))
                .show(sortItemFragmentHashMap.get(state)).commit();
        orderlistRlEntityList = new ArrayList<>();
        OrderlistRlEntity orderlistRlEntity = new OrderlistRlEntity();
        orderlistRlEntity.setName("全部");
        OrderlistRlEntity orderlistRlEntity1 = new OrderlistRlEntity();
        orderlistRlEntity1.setName("待付款");
        OrderlistRlEntity orderlistRlEntity2 = new OrderlistRlEntity();
        orderlistRlEntity2.setName("待发货");
        OrderlistRlEntity orderlistRlEntity3 = new OrderlistRlEntity();
        orderlistRlEntity3.setName("待收货");
        OrderlistRlEntity orderlistRlEntity4 = new OrderlistRlEntity();
        orderlistRlEntity4.setName("已完成");
        orderlistRlEntityList.add(orderlistRlEntity);
        orderlistRlEntityList.add(orderlistRlEntity1);
        orderlistRlEntityList.add(orderlistRlEntity2);
        orderlistRlEntityList.add(orderlistRlEntity3);
        orderlistRlEntityList.add(orderlistRlEntity4);
        orderlistRlEntityList.get(state).setSelect(true);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 5);
        rlV.setLayoutManager(gridLayoutManager);
        orderListPagerAdapeter = new OrderListPagerAdapeter(orderlistRlEntityList);
        orderListPagerAdapeter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.t_name:
                        setState(position);
                        break;
                }
            }
        });
        rlV.setAdapter(orderListPagerAdapeter);
    }

    private void setState(int position) {
        for (int a = 0; a < orderlistRlEntityList.size(); a++) {
            if (orderlistRlEntityList.get(a).isSelect()) {
                fragmentManager.beginTransaction().hide(sortItemFragmentHashMap.get(a)).commit();
                orderlistRlEntityList.get(a).setSelect(false);
            }

        }
        fragmentManager.beginTransaction().show(sortItemFragmentHashMap.get(position)).commit();
        orderlistRlEntityList.get(position).setSelect(true);
        orderListPagerAdapeter.notifyDataSetChanged();
    }
}
